package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Classinfo;
import cn.wolfcode.domain.Department;
import cn.wolfcode.domain.Employee;
import cn.wolfcode.qo.ClassinfoQueryObject;
import cn.wolfcode.qo.EmployeeQueryObject;
import cn.wolfcode.qo.JsonResult;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IClassinfoService;
import cn.wolfcode.service.IEmployeeService;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/classinfo")
public class ClassinfoController {
    @Autowired
    private IClassinfoService classinfoService;
    @Autowired
    private IEmployeeService employeeService;

    @RequestMapping("/list")
    public String list(Model model, ClassinfoQueryObject qo) {
        PageInfo<Classinfo> pageInfo = classinfoService.query(qo);
        List<Employee> employees = employeeService.listAll();
        List<Classinfo> classinfos = classinfoService.listAll();
        model.addAttribute("qo",qo);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("employees",employees);
        model.addAttribute("classinfos",classinfos);
        return "/classinfo/list";
    }

    @ResponseBody
    @RequestMapping("/delete")
    public JsonResult delete(Long id) {
        try {
            if (id != null) {
                classinfoService.delete(id);
            }
            return new JsonResult(true, "删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(false, "删除失败");
        }
    }


    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Classinfo classinfo) {
        try {
            if (classinfo.getId() == null) {
                classinfoService.save(classinfo);
            } else {
                classinfoService.update(classinfo);
            }
            return new JsonResult(true, "保存成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(false, "保存失败");
        }
    }


}
